package cn.thr.pethome.pet.controller;

import cn.thr.pethome.constants.BaseConstants;
import cn.thr.pethome.pet.service.IPetService;
import cn.thr.pethome.pet.domain.Pet;
import cn.thr.pethome.pet.query.PetQuery;
import cn.thr.pethome.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;



    /**
     * 批量下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }


    /**
     * 批量上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            String errorMsg = petService.onsale(ids);
            return AjaxResult.success().setMsg(errorMsg);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet,HttpServletRequest req){
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.add(pet,req);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        System.out.println(petService.findOne(id));
        return petService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody PetQuery query) {

        try {
            //获取到当前页
            List<Pet> byQuery = petService.findByQuery(query);
            //这是获取到总条数
            Long count = petService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
            //setData 用来返回数据的   setTotal 用来返回总条数
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    //查询商品是否上架
    @PostMapping("/lists")
    public AjaxResult jsons(@RequestBody PetQuery query) {

        try {
            //设置上架状态为1
            query.setState(BaseConstants.Pet.STATE_UP);
            //获取到当前页
            List<Pet> byQuery = petService.findByQuery(query);
            //这是获取到总条数
            Long count = petService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
            //setData 用来返回数据的   setTotal 用来返回总条数
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

}
